package craky.keeper;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import craky.component.JImagePane;
import craky.componentc.JCButton;
import craky.componentc.JCDialog;
import craky.componentc.JCLabel;
import craky.componentc.JCRadioButton;
import craky.keeper.util.KeeperUtil;
import craky.layout.LineLayout;
import craky.util.Config;
import craky.util.UIUtil;

public class SettingDialog extends JCDialog implements ActionListener,
		PopupMenuListener, ItemListener {
	private static final long serialVersionUID = 3367067393044735726L;
	private MainFrame mainFrame;
	private Config config;
	private JComponent content;
	private JCButton btnOk;
	private JCButton btnCancel;
	private JCButton btnApply;
	private JCLabel lbTabsFG;
	private JCLabel lbStatusFG;
	private JCRadioButton rbSkinMenuLeft;
	private JCRadioButton rbSkinMenuRight;
	private ColorChooserPopup colorPopup;
	private JCLabel currentColorLabel;
	private Color tabsFG;
	private Color statusFG;
	private boolean changed;

	public SettingDialog(KeeperApp keeper) {
		super(keeper.getMainFrame(), "设置", Dialog.ModalityType.DOCUMENT_MODAL);
		this.config = keeper.getConfig();
		this.mainFrame = keeper.getMainFrame();
		initUI();
		load();
		setVisible(true);
	}

	private void initUI() {
		ShowColorPopupListener colorListener = new ShowColorPopupListener();
		this.content = ((JComponent) getContentPane());
		JImagePane mainPane = new JImagePane();
		JImagePane buttonPane = new JImagePane();
		EmptyComponent ecColor = new EmptyComponent();
		JCLabel lbTabsFGTitle = new JCLabel("工具栏字体颜色：");
		JCLabel lbStatusFGTitle = new JCLabel("状栏字体颜色");
		this.lbTabsFG = new JCLabel();
		this.lbStatusFG = new JCLabel();
		this.btnOk = new JCButton("确定");
		this.btnCancel = new JCButton("取消");
		this.btnApply = new JCButton("应用");
		this.colorPopup = new ColorChooserPopup(null, new AbstractAction() {
			private static final long serialVersionUID = 8201411013412150507L;

			public void actionPerformed(ActionEvent e) {
				SettingDialog.this.changePurview(false);
			}
		});
		this.content.setBorder(new EmptyBorder(0, 2, 2, 2));
		this.content.setPreferredSize(new Dimension(512, 333));
		this.content.setLayout(new BorderLayout());
		mainPane.setLayout(new LineLayout(10, 15, 15, 15, 0, 10, 10, 1));
		mainPane.setBackground(new Color(255, 255, 255, 210));
		buttonPane.setLayout(new LineLayout(5, 7, 7, 7, 0, 10, 10, 0));
		buttonPane.setBackground(new Color(255, 255, 255, 150));
		buttonPane.setPreferredSize(new Dimension(-1, 32));
		ecColor.setLayout(new LineLayout(0, 10, 10, 0));
		ecColor.setPreferredSize(new Dimension(-1, 18));
		lbStatusFGTitle.setPreferredSize(new Dimension(135, -1));
		lbStatusFGTitle.setHorizontalAlignment(4);
		lbStatusFGTitle.setVerticalAlignment(3);
		lbTabsFGTitle.setVerticalAlignment(3);
		this.lbTabsFG.setBorder(new LineBorder(Color.BLACK));
		this.lbTabsFG.setBackgroundAlpha(1.0F);
		this.lbTabsFG.setPreferredSize(new Dimension(100, -1));
		this.lbTabsFG.addMouseListener(colorListener);
		this.lbStatusFG.setBorder(new LineBorder(Color.BLACK));
		this.lbStatusFG.setBackgroundAlpha(1.0F);
		this.lbStatusFG.setPreferredSize(this.lbTabsFG.getPreferredSize());
		this.lbStatusFG.addMouseListener(colorListener);
		this.btnOk.setPreferredSize(new Dimension(69, 21));
		this.btnOk.addActionListener(this);
		this.btnCancel.setPreferredSize(this.btnOk.getPreferredSize());
		this.btnCancel.addActionListener(this);
		this.btnApply.setPreferredSize(this.btnOk.getPreferredSize());
		this.btnApply.addActionListener(this);
		this.colorPopup.addPopupMenuListener(this);

		ecColor.add(lbTabsFGTitle, "StartFill");
		ecColor.add(this.lbTabsFG, "StartFill");
		ecColor.add(lbStatusFGTitle, "StartFill");
		ecColor.add(this.lbStatusFG, "StartFill");
		mainPane.add(ecColor, "StartFill");
		mainPane.add(createSkinMenuPurview(), "StartFill");
		buttonPane.add(this.btnOk, "End");
		buttonPane.add(this.btnCancel, "End");
		buttonPane.add(this.btnApply, "End");
		this.content.add(buttonPane, "South");
		this.content.add(mainPane, "Center");
		setDefaultCloseOperation(2);
		pack();
		setResizable(false);
		setLocationRelativeTo(getOwner());
		UIUtil.escAndEnterAction(this, this.btnOk, new AbstractAction() {
			private static final long serialVersionUID = -933736543655670099L;

			public void actionPerformed(ActionEvent e) {
				SettingDialog.this.cancel();
			}
		});
	}

	private JComponent createSkinMenuPurview() {
		ButtonGroup group = new ButtonGroup();
		EmptyComponent ec = new EmptyComponent();
		EmptyComponent ecButton = new EmptyComponent();
		EmptyComponent ecImage = new EmptyComponent();
		JCLabel lbPos = new JCLabel("皮肤菜单显示位置");
		this.rbSkinMenuLeft = new JCRadioButton("按钮右对");
		this.rbSkinMenuRight = new JCRadioButton("窗口右对");
		JImagePane leftPurview = new JImagePane(
				KeeperUtil.getImage("skin_popup_left.png"), "Tiled");
		JImagePane rightPurview = new JImagePane(
				KeeperUtil.getImage("skin_popup_right.png"), "Tiled");
		ec.setPreferredSize(new Dimension(-1, 250));
		ec.setLayout(new LineLayout(5, 0, 0, 0, 0, 10, 10, 1));
		ecButton.setPreferredSize(new Dimension(480, 18));
		ecButton.setLayout(new GridLayout(1, 2, 8, 0));
		ecImage.setPreferredSize(new Dimension(480, 172));
		ecImage.setLayout(new GridLayout(1, 2, 8, 0));
		lbPos.setPreferredSize(new Dimension(-1, 18));
		lbPos.setVerticalAlignment(3);
		this.rbSkinMenuLeft.addItemListener(this);
		this.rbSkinMenuRight.addItemListener(this);
		group.add(this.rbSkinMenuLeft);
		group.add(this.rbSkinMenuRight);
		ecImage.add(leftPurview);
		ecImage.add(rightPurview);
		ecButton.add(this.rbSkinMenuLeft);
		ecButton.add(this.rbSkinMenuRight);
		ec.add(lbPos, "StartFill");
		ec.add(ecButton, "Start");
		ec.add(ecImage, "Start");
		return ec;
	}

	private void load() {
		this.tabsFG = KeeperUtil.stringToColor(this.config.getProperty(
				"Tabs_Foreground", "225,242,250"));
		this.statusFG = KeeperUtil.stringToColor(this.config.getProperty(
				"Status_Foreground", "225,242,250"));
		int skinPopupX = Integer.parseInt(this.config.getProperty(
				"Skin_Popup_X", "0"));

		if (skinPopupX == -216) {
			this.rbSkinMenuLeft.setSelected(true);
		} else if (skinPopupX == -130) {
			this.rbSkinMenuRight.setSelected(true);
		}

		this.lbTabsFG.setBackground(this.tabsFG);
		this.lbStatusFG.setBackground(this.statusFG);
		this.btnApply.setEnabled(this.changed);
	}

	private void changePurview(boolean submit) {
		Color color = null;

		if (submit) {
			this.btnApply.setEnabled(this.changed = (!this.changed)
					&& (this.colorPopup.isCanceled()) ? false : true);
			color = this.colorPopup.getColor();
		} else {
			color = this.colorPopup.getCurrentColor();
		}

		this.currentColorLabel.setBackground(color);

		if (this.currentColorLabel == this.lbTabsFG) {
			this.mainFrame.setTabForeground(color);
		} else if (this.currentColorLabel == this.lbStatusFG) {
			this.mainFrame.setStatusForeground(color);
		}
	}

	private void submit() {
		if (!this.changed) {
			return;
		}

		Integer skinPopupX = null;

		if (this.rbSkinMenuLeft.isSelected()) {
			skinPopupX = Integer.valueOf(-216);
		} else if (this.rbSkinMenuRight.isSelected()) {
			skinPopupX = Integer.valueOf(-130);
		}

		if (skinPopupX != null) {
			this.config.setPropertie("Skin_Popup_X", skinPopupX.toString());
			this.mainFrame.resetSkinPopupLocation();
		}

		this.config.setPropertie("Tabs_Foreground", KeeperUtil
				.colorToString(this.tabsFG = this.lbTabsFG.getBackground()));
		this.config
				.setPropertie("Status_Foreground", KeeperUtil
						.colorToString(this.statusFG = this.lbStatusFG
								.getBackground()));
		this.config.saveConfig();
		this.btnApply.setEnabled(this.changed == false);
	}

	private void cancel() {
		if (this.changed) {
			this.mainFrame.setTabForeground(this.tabsFG);
			this.mainFrame.setStatusForeground(this.statusFG);
		}

		dispose();
	}

	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();

		if (source == this.btnOk) {
			submit();
			dispose();
		} else if (source == this.btnApply) {
			submit();
		} else if (source == this.btnCancel) {
			cancel();
		}
	}

	public void itemStateChanged(ItemEvent e) {
		this.btnApply.setEnabled(this.changed = (!this.changed)
				&& (e.getStateChange() != 1) ? false : true);
	}

	public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
		changePurview(true);
	}

	public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
	}

	public void popupMenuCanceled(PopupMenuEvent e) {
	}

	private class ShowColorPopupListener extends MouseAdapter {
		private ShowColorPopupListener() {
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			if (SwingUtilities.isLeftMouseButton(e)) {
				SettingDialog.this.currentColorLabel = ((JCLabel) e.getSource());
				SettingDialog.this.colorPopup.setColor(
						SettingDialog.this.currentColorLabel.getBackground(),
						true);

				if (SettingDialog.this.currentColorLabel == SettingDialog.this.lbTabsFG) {
					SettingDialog.this.colorPopup.show(
							SettingDialog.this.currentColorLabel, -5,
							SettingDialog.this.currentColorLabel.getHeight());
				} else if (SettingDialog.this.currentColorLabel == SettingDialog.this.lbStatusFG) {
					SettingDialog.this.colorPopup.show(
							SettingDialog.this.currentColorLabel,
							SettingDialog.this.currentColorLabel.getWidth()
									- SettingDialog.this.colorPopup
											.getPreferredSize().width + 5,
							SettingDialog.this.currentColorLabel.getHeight());
				}
			}
		}
	}
}